package org.amphiprion.descentv2assistant.adapter;

import java.util.HashMap;
import java.util.List;

import org.amphiprion.descentv2assistant.App;
import org.amphiprion.descentv2assistant.ApplicationConstants;
import org.amphiprion.descentv2assistant.R;
import org.amphiprion.descentv2assistant.entity.Hero;
import org.amphiprion.descentv2assistant.ui.views.PinnedHeaderListView;
import org.amphiprion.descentv2assistant.ui.views.PinnedHeaderListView.PinnedHeaderAdapter;

import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;

public class HeroListAdapter extends BaseAdapter implements PinnedHeaderListView.PinnedHeaderAdapter, SectionIndexer, AbsListView.OnScrollListener {
	public static final int SMALL = 2;
	protected FragmentActivity mContext;
	private HeroIndexer mIndexer;
	protected LayoutInflater mInflater;
	protected List<Hero> mItems;
	private int mMode;
	private HashMap<String, Integer> mHeroCountMap;

	public HeroListAdapter(FragmentActivity paramFragmentActivity, List<Hero> heroes) {
		this.mContext = paramFragmentActivity;
		this.mInflater = LayoutInflater.from(paramFragmentActivity);
		this.mItems = heroes;
		initItems();
	}

	private void bindSectionHeader(View paramView, int heroIndex) {
		Hero hero = this.mItems.get(heroIndex);

		View headerView = paramView.findViewById(R.id.header);
		ImageView img = (ImageView) paramView.findViewById(R.id.header_image);
		App.setImage(img, hero.getArchetype());

		TextView sectionTitle = (TextView) paramView.findViewById(R.id.header_text);
		TextView numberOfEpisodeInSection = (TextView) paramView.findViewById(R.id.totalCount);
		View headerDividerView = paramView.findViewById(R.id.divider);

		// Check box for watched
		// initSeasonTicker(episode, paramView, true);

		// Check if we need to display the header

		int i = getSectionForPosition(heroIndex);
		Object[] arrayOfObject;
		if (getPositionForSection(i) == heroIndex) {
			arrayOfObject = getSections();
			try {
				sectionTitle.setText((String) arrayOfObject[i]);
				headerView.setVisibility(0);
				numberOfEpisodeInSection.setText(String.valueOf(getHeroCount(hero.getArchetype().getName())));
				headerDividerView.setVisibility(0);
				// if (-1 + getPositionForSection(i + 1) == episodeIndex) {
				// headerView.setVisibility(8);
				// headerDividerView.setVisibility(8);
				// }
				return;
			} catch (Exception ex) {
				Log.e(ApplicationConstants.PACKAGE, "", ex);
			}
		} else {
			headerView.setVisibility(8);
			headerDividerView.setVisibility(8);
		}
	}

	private int getHeroCount(String sectionName) {
		return this.mHeroCountMap.get(sectionName).intValue();
	}

	private void inflateView(View view, final int index, ViewHolder paramViewHolder) {
		final Hero localHero = getItem(index);
		paramViewHolder.mName.setText(localHero.getName());
		paramViewHolder.mStatMight.setText("" + localHero.getMight());
		paramViewHolder.mStatKnowledgre.setText("" + localHero.getKnowledge());
		paramViewHolder.mStatWillpower.setText("" + localHero.getWillpower());
		paramViewHolder.mStatAwareness.setText("" + localHero.getAwareness());

		paramViewHolder.mCharacSpeed.setText("" + localHero.getCharacteristic().getSpeed());
		paramViewHolder.mCharacHealth.setText("" + localHero.getCharacteristic().getHealth());
		paramViewHolder.mCharacStamina.setText("" + localHero.getCharacteristic().getStamina());
		// paramViewHolder.mCharacDefense.setText(localHero.getCharacteristic().getDefense());
		paramViewHolder.mCharacDefense.removeAllViews();
		String[] dice = localHero.getCharacteristic().getDefense().split(",");
		for (String die : dice) {
			int nb = 1;
			try {
				nb = Integer.parseInt(die.substring(0, 1));
			} catch (Throwable e) {
			}
			for (int i = 0; i < nb; i++) {
				ImageView img = (ImageView) this.mInflater.inflate(R.layout.image_dice, null);
				String name = die.substring(1);
				App.setImage(img, "/images/DiceDefense/", name);
				paramViewHolder.mCharacDefense.addView(img);
			}
		}

		paramViewHolder.mAbility.setText(localHero.getAbility().getDescription());
		if (localHero.getAbility().isAction()) {
			paramViewHolder.mAbilityAction.setVisibility(View.VISIBLE);
		} else {
			paramViewHolder.mAbilityAction.setVisibility(View.GONE);
		}
		paramViewHolder.mFeat.setText(localHero.getFeat().getDescription());
		if (localHero.getFeat().isAction()) {
			paramViewHolder.mFeatAction.setVisibility(View.VISIBLE);
		} else {
			paramViewHolder.mFeatAction.setVisibility(View.GONE);
		}

		App.setImage(paramViewHolder.mScreencap, localHero, mContext.getSupportFragmentManager());

		bindSectionHeader(view, index);

	}

	private void initItems() {
		this.mIndexer = new HeroIndexer(this.mContext, this.mItems);
		this.mHeroCountMap = new HashMap();
		for (int i = 0; i < this.mItems.size(); i++) {
			String sectionName = this.mItems.get(i).getArchetype().getName();
			Integer localInteger = this.mHeroCountMap.get(sectionName);
			if (localInteger == null) {
				this.mHeroCountMap.put(sectionName, Integer.valueOf(1));

			} else {
				this.mHeroCountMap.put(sectionName, Integer.valueOf(1 + localInteger.intValue()));
			}

			// if (this.mSeasonWatchMap.get(Integer.valueOf(seasonId)) != null) {
			// if ((this.mSeasonWatchMap.get(Integer.valueOf(seasonId)).booleanValue())) {
			// this.mSeasonWatchMap.put(Integer.valueOf(seasonId), Boolean.valueOf(this.mItems.get(i).isWatched()));
			// }
			// } else {
			// this.mSeasonWatchMap.put(Integer.valueOf(seasonId), Boolean.valueOf(this.mItems.get(i).isWatched()));
			// }
		}
	}

	// private void initSeasonTicker(final Episode paramEpisode, View paramView, boolean paramBoolean) {
	// ImageView localImageView = (ImageView) paramView.findViewById(R.id.watch_season);
	// int i = paramEpisode.getSeasonId();
	// Boolean seasonWatched = this.mSeasonWatchMap.get(Integer.valueOf(i));
	// if (!seasonWatched.booleanValue()) {
	// localImageView.setImageResource(R.drawable.ic_watched_grey);
	// // localImageView.setBackgroundResource(R.drawable.holo_selector);
	// localImageView.setOnClickListener(new View.OnClickListener() {
	// @Override
	// public void onClick(View paramAnonymousView) {
	// EpisodeDiffusionListAdapter.this.tickEpisode(paramEpisode, true);
	// }
	// });
	// return;
	// } else {
	// localImageView.setImageResource(R.drawable.ic_watched_blue);
	// // if (!paramBoolean) {
	// // localImageView.setImageResource(R.drawable.ic_watched_blue);
	// // localImageView.setBackgroundResource(R.drawable.holo_selector);
	// // } else {
	// // localImageView.setImageResource(R.drawable.green_tick_selector);
	// // localImageView.setBackgroundResource(R.drawable.green_selector);
	// // }
	// localImageView.setOnClickListener(new View.OnClickListener() {
	// @Override
	// public void onClick(View paramAnonymousView) {
	// EpisodeDiffusionListAdapter.this.tickEpisode(paramEpisode, false);
	// }
	// });
	// }
	//
	// }

	private void setHero(int index, Hero hero) {
		this.mItems.set(index, hero);
		initItems();
		notifyDataSetChanged();
	}

	// private void tickEpisode(Episode paramEpisode, boolean watched) {
	// if (this.mSeasonWatchListener != null) {
	// int serieId = paramEpisode.getSerieId();
	// String seasonId = "" + paramEpisode.getSeasonId();
	// this.mSeasonWatchListener.onSeasonWatch(serieId, seasonId, watched);
	// }
	// }

	@Override
	public void configurePinnedHeader(View view, int index, int paramInt2) {
		Hero localHero = this.mItems.get(index);
		ImageView img = (ImageView) view.findViewById(R.id.header_image);
		App.setImage(img, localHero.getArchetype());
		TextView localTextView1 = (TextView) view.findViewById(R.id.header_text);
		TextView localTextView2 = (TextView) view.findViewById(R.id.totalCount);
		// initSeasonTicker(localEpisode, paramView, false);
		int i = getSectionForPosition(index);

		localTextView2.setText(String.valueOf(getHeroCount(localHero.getArchetype().getName())));
		String sectionTitle = (String) this.mIndexer.getSections()[i];
		int j = App.dpiToPixels(mContext, 8);
		localTextView1.setPadding(j, j, j, j);
		localTextView1.setText(sectionTitle);
		view.setVisibility(0);
		return;

	}

	@Override
	public int getCount() {
		if (this.mItems != null) {
			return this.mItems.size();
		} else {
			return 0;
		}
	}

	@Override
	public Hero getItem(int paramInt) {
		return this.mItems.get(paramInt);
	}

	@Override
	public long getItemId(int paramInt) {
		return paramInt;
	}

	@Override
	public int getPinnedHeaderState(int position) {
		int i = PinnedHeaderAdapter.PINNED_HEADER_GONE;
		if ((this.mIndexer == null) || (getCount() == 0)) {

		} else {
			if (position >= 0) {

				int j = getPositionForSection(1 + getSectionForPosition(position));
				if ((j != -1) && (position == j - 1)) {
					i = PinnedHeaderAdapter.PINNED_HEADER_PUSHED_UP;
				} else {
					i = PinnedHeaderAdapter.PINNED_HEADER_VISIBLE;
				}
			}
		}
		return i;
	}

	@Override
	public int getPositionForSection(int paramInt) {
		try {
			return this.mIndexer.getPositionForSection(paramInt);
		} catch (Exception localException) {
			return 0;
		}
	}

	@Override
	public int getSectionForPosition(int paramInt) {
		try {
			int i = this.mIndexer.getSectionForPosition(paramInt);
			return i;
		} catch (Exception localException) {
			int i = 0;
			return i;
		}
	}

	@Override
	public Object[] getSections() {
		return this.mIndexer.getSections();
	}

	@Override
	public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
		ViewHolder localViewHolder;
		int i;
		if (paramView == null) {
			localViewHolder = new ViewHolder();
			LayoutInflater localLayoutInflater = this.mInflater;
			// if (this.mMode == SMALL) {
			i = R.layout.list_item_hero;
			// } else {
			// i = R.layout.list_item_episode;
			// }
			paramView = localLayoutInflater.inflate(i, null);
			localViewHolder.mName = ((TextView) paramView.findViewById(R.id.heroName));
			localViewHolder.mStatMight = ((TextView) paramView.findViewById(R.id.heroMight));
			localViewHolder.mStatKnowledgre = ((TextView) paramView.findViewById(R.id.heroKnowledge));
			localViewHolder.mStatWillpower = ((TextView) paramView.findViewById(R.id.heroWillpower));
			localViewHolder.mStatAwareness = ((TextView) paramView.findViewById(R.id.heroAwarness));

			localViewHolder.mCharacSpeed = ((TextView) paramView.findViewById(R.id.heroMove));
			localViewHolder.mCharacHealth = ((TextView) paramView.findViewById(R.id.heroHealth));
			localViewHolder.mCharacStamina = ((TextView) paramView.findViewById(R.id.heroStamina));
			localViewHolder.mCharacDefense = ((LinearLayout) paramView.findViewById(R.id.heroDefense));

			localViewHolder.mScreencap = ((ImageView) paramView.findViewById(R.id.screenCap));
			localViewHolder.mAbility = ((TextView) paramView.findViewById(R.id.ability));
			localViewHolder.mAbilityAction = ((ImageView) paramView.findViewById(R.id.ability_action));
			localViewHolder.mFeat = ((TextView) paramView.findViewById(R.id.feat));
			localViewHolder.mFeatAction = ((ImageView) paramView.findViewById(R.id.feat_action));

			paramView.setTag(localViewHolder);
		} else {
			localViewHolder = (ViewHolder) paramView.getTag();
		}

		inflateView(paramView, paramInt, localViewHolder);
		return paramView;
	}

	@Override
	public void onScroll(AbsListView paramAbsListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		if (((paramAbsListView instanceof PinnedHeaderListView))) {
			((PinnedHeaderListView) paramAbsListView).configureHeaderView(firstVisibleItem);
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView paramAbsListView, int scrollState) {

	}

	public void setItems(List<Hero> heroes) {
		this.mItems = heroes;
		initItems();
		notifyDataSetChanged();
	}

	public void setMode(int paramInt) {
		this.mMode = paramInt;
	}

	// public void setOnSeasonWatch(OnWatchSeasonListener paramOnWatchSeasonListener) {
	// this.mSeasonWatchListener = paramOnWatchSeasonListener;
	// }

	class ViewHolder {
		TextView mName;
		TextView mStatMight;
		TextView mStatKnowledgre;
		TextView mStatWillpower;
		TextView mStatAwareness;

		TextView mCharacSpeed;
		TextView mCharacHealth;
		TextView mCharacStamina;
		LinearLayout mCharacDefense;

		public ImageView mScreencap;
		public TextView mAbility;
		public ImageView mAbilityAction;
		public TextView mFeat;
		public ImageView mFeatAction;

		ViewHolder() {
		}
	}

	// public static abstract interface OnWatchSeasonListener {
	// public abstract void onSeasonWatch(int serieId, String seasonId, boolean watched);
	// }
}